package com.ea.eadp.nexus.test.pageDefinition.mobile;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.common.TestUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;


public class SignUpPage extends MobileBasePage {
    public final static String signUpPage = "createPage";
    public static final String emailInputBox = "register-email";
    public static final String originIdInputBox = "register-originId";
    public static final String passwordInputBox = "register-password";

    public SignUpPage(WebDriver driver) {
        super(driver, SignUpPage.class, signUpPage);
    }

    public void submit() {
        log.info("Sign Up");
        driver.findElement(By.xpath("//div[@id='submitBtn']/div[1]/input")).click();
    }

    public void enterEmail(String email) {
        log.info("Enter Email:" + email);
        sendInput(emailInputBox, email);
        driver.findElement(By.xpath("//div[@id='createPage']/div[1]/h1")).click();
    }

    public void validateEmail(String email, String errorMessage) {
        log.info("Validate Email:" + email);
        sendInput(emailInputBox, email);
        driver.findElement(By.xpath("//div[@id='createPage']/div[1]/h1")).click();
        TestUtil.sleep(1);
        Assert.assertEquals(getEmailInvalidError(), errorMessage, "Input not trigger the correct error message");
    }

    public void enterOriginId(String originId) {
        log.info("Enter Origin Id:" + originId);
        sendInput(originIdInputBox, originId);
        driver.findElement(By.xpath("//div[@id='createPage']/div[1]/h1")).click();
    }

    public void validateOriginId(String originId, String errorMessage) {
        log.info("Validate Origin Id:" + originId);
        sendInput(originIdInputBox, originId);
        driver.findElement(By.xpath("//div[@id='createPage']/div[1]/h1")).click();
        TestUtil.sleep(1);
        Assert.assertEquals(getOriginIdInvalidError(), errorMessage, "Input not trigger the correct error message");
    }

    public void enterPassword(String password) {
        log.info("Enter Password:" + password);
        sendInput(passwordInputBox, password);
        driver.findElement(By.xpath("//div[@id='createPage']/div[1]/h1")).click();
    }

    public void validatePassword(String password, String errorMessage) {
        log.info("Validate Password:" + password);
        sendInput(passwordInputBox, password);
        driver.findElement(By.xpath("//div[@id='createPage']/div[1]/h1")).click();
        TestUtil.sleep(1);
        Assert.assertEquals(getPasswordInvalidError(), errorMessage, "Input not trigger the correct error message");
    }

    public void enterDob(String year, String month, String day) {
        clickBlank("//div[@id='register-dob']/div/span");
        Select selectYear = new Select(driver.findElement(By.id("dob-select-year")));
        Select selectMonth = new Select(driver.findElement(By.id("dob-select-month")));
        Select selectDay = new Select(driver.findElement(By.id("dob-select-day")));
        selectYear.selectByValue(year);
        selectMonth.selectByValue(month);
        selectDay.selectByValue(day);
    }

    public void selectTos() {
        driver.findElement(By.xpath("//*[@id='security-preferences']/div[1]/fieldset/div/div/label/span/span[2]")).click();
    }

    public String getEmailInvalidError() {
        return getElementStringByXPath("//form[@id='createForm']/div[1]/div[1]/span[2]");
    }

    public String getOriginIdInvalidError() {
        return getElementStringByXPath("//form[@id='createForm']/div[1]/div[2]/span[2]");
    }

    public String getPasswordInvalidError() {
        return getElementStringByXPath("//form[@id='createForm']/div[1]/div[3]/span[3]");
    }

    public String getSignUpResponse() {
        TestUtil.sleep(1);
        PageOperation.WebDriverWaitByXpath(driver, "//div[@id='create-popup']/div[2]/div[1]");
        return getElementStringByXPath("//div[@id='create-popup']/div[2]/div[1]");
    }

    public void selectContactMe(boolean check) {
        driver.findElement(By.xpath("//div[@id='security-preferences']/div[2]/fieldset/div/div/label/span")).click();
    }

    public void selectShareInfo(boolean check) {
        driver.findElement(By.xpath("//div[@id='security-preferences']/div[3]/fieldset/div/div/label/span")).click();
    }

    public String getContactMeLabel() {
        return getElementStringByXPath("//div[@id='security-preferences']/div[2]/fieldset/div/div/label/span/span");
    }

    public String getShareInfoLabel() {
        return getElementStringByXPath("//div[@id='security-preferences']/div[3]/fieldset/div/div/label/span/span");
    }
}

